<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class IndexController extends CommonController {
	protected $table = 'admin';
	protected $st_join = 'store_joinin';
	protected $sp_user = 'user';	
	protected $tb_adv = 'adv';//广告表
	protected $tb_store = 'store';//店铺表
	protected $tb_goods = 'product';//产品表
	protected $tb_prom = 'promotion';//促销表

	/**
	*后台系统首页
	*/
    public function index(){
		//店铺
		//开店审核数量
		unset($str);
		unset($data);
		$where = "joinin_state<>40";
		$store_j = M($this->st_join)->where($where)->count();
		//会员数量
		unset($where);
		$where = "username<>''";
		$user_num = M($this->sp_user)->where($where)->count();
		//dump($_SESSION);die;
		$this->assign('user_num',$user_num);
		$this->assign('store_j',$store_j);
        $this->display();
    }

	/*
	*首页幻灯片
	*/
    public function home(){
		unset($str);
		unset($data);
		unset($cache);
		$cache = 'home_adv' ;
		$str = 'adv_id>%d and is_allow=%d and adv_type=%d' ;
		$data= array(0,1,11) ;

		if (I('get.adv_add') || I('get.id')) {
			
			if (I('get.id')) {
				//修改
				$where_1['adv_id'] = I('get.id');
				$adlist = _getOne($this->tb_adv,$where_1);
				$adlist['adv_pic'] = explode('||',$adlist['adv_pic']) ;
				$adlist['adv_url'] = explode('||',$adlist['adv_url']) ;
				//dump($adlist);die;
				if (I('get.pid')) {
					//四张联动焦点图修改
					$type = 'adv_edit_4';
					$adv_type = 1 ;
				} else {
					$adlist['adv_pic'] = $adlist['adv_pic'] [0] ;
					$adlist['adv_url'] = $adlist['adv_url'] [0] ;
					$adv_type = 11 ;
					$type = 'adv_edit';
				}

				$this->assign('adlist',$adlist) ;
			} else {
				//新增
				$type = I('get.adv_add');
			}
			
			$this->assign('type',$type) ;
			
			if (I('get.html')) {				
				$adv_type = 11 ;
				if (I('get.adv_add')=='adv_add_4' || I('get.html')=='home_four') {
					//四张联动焦点图
					
					unset($str);
					unset($data);
					unset($cache);
					$cache = 'home_adv_4' ;
					$str = 'adv_id>%d and is_allow=%d and adv_type=%d' ;
					$data= array(0,1,1) ;	
					$adlist_4 = _getList($this->tb_adv,$str,$data,$cache,'slide_sort desc') ;
					foreach ($adlist_4 as $key=>$v) {
						$v['adv_pic'] = explode('||',$v['adv_pic']) ;
						$v['adv_url'] = explode('||',$v['adv_url']) ;
						$adlist_4 [$key] ['adv_pic'] = $v['adv_pic'] ;
						$adlist_4 [$key] ['adv_url'] = $v['adv_url'] ;
					}
					//S($cache,null);
					unset($adv_type);
					$adv_type = 1 ;	
					$this->assign('adlist_4',$adlist_4) ;
				}
				
				$this->assign('adv_type',$adv_type) ;
				$this->display(I('get.html'));
			} else {
				$this->assign('adv_type',$adv_type) ;
				$this->display('adv_add');
			}
		} else {
			
			$adlist = _getList($this->tb_adv,$str,$data,$cache,'slide_sort desc') ;
			
			$this->assign('adlist',$adlist) ;
			$this->display();		
		}		

    }
	
	/*
	*首页幻灯片 广告添加/修改
	*/
    public function adv_add(){
	
		if (IS_POST) {
			
			$type = I('post.type');
			unset($data);
			switch ($type) {
				case 'adv_add':
					$url = 'home';
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('文字标题',$url);
					
					$data['adv_url'] = I('post.adv_url') ;		
				    $data['adv_pic'] = upload();
					I('post.slide_sort') ? $data['slide_sort'] = I('post.slide_sort'):$data['slide_sort'] = 255;
					$data['adv_type'] = I('post.adv_type') ;	
					
					$data['is_allow'] = 1;	
					$data['adv_start_date'] = time();	
					
					$res = 1 ;
					$massege = '添加成功!' ;
					$error = '添加失败！' ;
					$table = $this->tb_adv ;
				break;
				case 'adv_add_4':
					$url = 'home';
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('文字标题',$url);
					$url_pic = I('post.adv_url') ;
					$data['adv_url'] = $url_pic[0].'||'. $url_pic[1].'||'. $url_pic[2].'||'. $url_pic[3];		
				    $pic = upload();
					
					$data['adv_pic'] = $pic['adv_pic1'].'||'.$pic['adv_pic2'].'||'.$pic['adv_pic3'].'||'.$pic['adv_pic4'] ;
					
					I('post.slide_sort') ? $data['slide_sort'] = I('post.slide_sort'):$data['slide_sort'] = 255;
					$data['adv_type'] = I('post.adv_type') ;	
					
					$data['is_allow'] = 1;	
					$data['adv_start_date'] = time();	
					
					$res = 1 ;
					$massege = '添加成功!' ;
					$error = '添加失败！' ;
					$table = $this->tb_adv ;
				break;
				case 'adv_edit':
					$url = 'home';
					$where ['adv_id'] = I('post.adv_id');
					
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('文字标题',$url);
					
					$data['adv_url'] = I('post.adv_url') ;
					
					$pic = upload();
					
					if ($pic) {
						$data['adv_pic'] = $pic;
					}
					
					I('post.slide_sort') ? $data['slide_sort'] = I('post.slide_sort'):$data['slide_sort'] = 255;	
					
					$res = 2 ;
					$massege = '修改成功!' ;
					$error = '修改失败！' ;
					$table = $this->tb_adv ;					
				break;
				case 'adv_edit_4':
					$url = 'home';
					$where ['adv_id'] = I('post.adv_id');
					$table = $this->tb_adv ;
					
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('文字标题',$url);
					
					$data['adv_url'] = I('post.adv_url') ;
					
					$url_pic = I('post.adv_url') ;
					$data['adv_url'] = $url_pic[0].'||'. $url_pic[1].'||'. $url_pic[2].'||'. $url_pic[3];
					
				    $pic = upload();

					if ($pic) {
						
						$adlist = _getOne($table,$where);
						$adlist['adv_pic'] = explode('||',$adlist['adv_pic']) ;
						//dump($adlist);
						!$pic['adv_pic1'] && $pic['adv_pic1'] = $adlist['adv_pic'][0] ; 
						!$pic['adv_pic2'] && $pic['adv_pic2'] = $adlist['adv_pic'][1] ;
						!$pic['adv_pic3'] && $pic['adv_pic3'] = $adlist['adv_pic'][2] ;
						!$pic['adv_pic4'] && $pic['adv_pic4'] = $adlist['adv_pic'][3] ;
						
						$data['adv_pic'] = $pic['adv_pic1'].'||'.$pic['adv_pic2'].'||'.$pic['adv_pic3'].'||'.$pic['adv_pic4'] ;
													
					}
					
					I('post.slide_sort') ? $data['slide_sort'] = I('post.slide_sort'):$data['slide_sort'] = 255;	
					
					$res = 2 ;
					$massege = '修改成功!' ;
					$error = '修改失败！' ;
										
				break;
				case 'h_adv_add':
					$url = 'home_ad';
					
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('标题不能为空',$url);
	
					I('post.adv_url') && $data['adv_url'] = I('post.adv_url') ;
					
					$pic = upload();
					
					if ($pic) {
						$data['adv_pic'] = $pic;
					}	
					
					I('post.adv_start_date') && $data['adv_start_date'] = strtotime(I('post.adv_start_date')) ;
					I('post.adv_end_date') && $data['adv_end_date'] = strtotime(I('post.adv_end_date')) ;
					$data['adv_type'] = I('post.adv_type') ;
					$data['ap_id'] = I('post.ap_id') ;
					I('post.goldpay') && $data['goldpay'] = I('post.goldpay') ;
					I('post.adv_states') && $data['adv_states'] = I('post.adv_states') ;
					
					$res = 1 ;
					$massege = '新增成功!' ;
					$error = '新增失败！' ;
					$table = $this->tb_adv ;					
				break;	
				case 'h_adv_edit':
					$url = 'home_ad';
					$where ['adv_id'] = I('post.adv_id');
					
					I('post.adv_title') ? $data['adv_title'] = I('post.adv_title'):$this->error('标题不能为空',$url);
					
					$data['adv_url'] = I('post.adv_url') ;
					
					$pic = upload();
					
					if ($pic) {
						$data['adv_pic'] = $pic;
					}
					
					I('post.adv_start_date') && $data['adv_start_date'] = strtotime(I('post.adv_start_date')) ;
					I('post.adv_end_date') && $data['adv_end_date'] = strtotime(I('post.adv_end_date')) ;
					
					$data['adv_type'] = I('post.adv_type') ;
					$data['ap_id'] = I('post.ap_id') ;
					I('post.goldpay') && $data['goldpay'] = I('post.goldpay') ;
					I('post.adv_states') && $data['adv_states'] = I('post.adv_states') ;
					
					$res = 2 ;
					$massege = '修改成功!' ;
					$error = '修改失败！' ;
					$table = $this->tb_adv ;					
			  break;				
			}
			
			//dump($data);die;
			if ($res==1) {
				$change = _getAdd($table,$data);
			} else if ($res==2) {				
				$change =  _getEdit($table,$where,'',$data);

			}			
				
			unset($data);
			unset($res);
			unset($type);	
			unset($where);
			unset($table);
			
			if ($change) {
				//清楚缓存
				SClear() ;
				unset($change);
				$this->success($massege,$url);				
			} else {
				$this->error($error,$url);
			}			
		}
    }
	 
	/**
	*广告管理
	*/	 
	 public function home_ad(){
		if (I('get.type')==='h_adv_add' || I('get.type')==='h_adv_edit') {
			$this->homeadv_add();
		} else {
			$str = 'adv_id>%d and adv_type=%d' ;
			$data= array(0,2) ;	
			$list = M($this->tb_adv)->where($str, $data)->select();
			foreach ($list as $key => $v) {
				unset($cache);
				$cache = 'home_adv_'.$v['ap_id'] ;
				if (!S($cache)) {
					S($cache,$v);
				}				
			}
			//dump($list);die;
			$this->assign('list',$list);
			$this->display();
		}
        
    }
	
	/**
	*广告增、删、改
	*/	 
	 public function homeadv_add(){
		 
		if (I('get.adv_id')) {
			$str = 'adv_id=%d' ;
			$data= array(I('get.adv_id')) ;	
			$list = M($this->tb_adv)->where($str, $data)->find();			
		}
		
		$this->assign('adlist',$list); 
		$this->assign('adv_type',2); 
		$this->assign('type',I('get.type'));
		$this->display('homeadv_add');       
    }

	/**
	*首页促销区
	*/	 
	 public function home_sales(){
		 unset($str) ;
		 unset($data) ;
		 unset($cache) ;
		 //添加修改页 
		 if (I('get.sales_add') || I('get.select')) {
			 //添加
			 $type = 'prom_add' ;
			 if (I('get.id')) {
				$type = 'prom_edit' ; 
				$prom_one = _getOne($this->tb_prom,array('se_id'=>I('get.id')));
				$prom_one ['g_id'] = strAnalysis($prom_one['g_id']) ;
				//商品查询
				foreach ($prom_one['g_id'] as $key=>$vv) {
					unset($goods_oen) ;
					$goods_oen = _getOne($this->tb_goods,array('id'=>$vv));
					 $prom_one['goods'][] = $goods_oen ;
				}
				//dump($prom_one);die;
				$this->assign('prom_one',$prom_one) ;
			 }
			 $this->assign('type',$type) ;
			 $this->h_sales_add();
		 } else {
			 
			//首页 
			$cache = 'prom_list_1' ;
			$str = 'se_id>%d and se_type' ;
			$data = array(0,1) ;
			S($cache,null) ;
			if (S($cache)) {
				$prom_list = S($cache) ;
			} else {
				$prom_list = _getList($this->tb_prom,$str,$data,'','se_id desc') ; 
				foreach($prom_list as $k=>$v) {
					$prom_list [$k]['g_id'] = strAnalysis($v['g_id']) ;
					//商品查询
					foreach ($prom_list [$k]['g_id'] as $key=>$vv) {
						 unset($goods) ;
						 $goods = _getOne($this->tb_goods,array('id'=>$vv));
						 $prom_list [$k]['goods'][] = $goods ;
					}
					
				}
				S($cache,$prom_list) ;				
			}

			//dump($prom_list);die;
			$this->assign('prom_list',$prom_list) ;
			$this->display(); 
		 }      
    }
	
	/**
	*首页促销区 添加/修改页面 搜索
	*/		
	public function h_sales_add() {
		
		//搜索商品
		if (I('get.select')) {
			unset($where);
			unset($str);
			unset($data);
			$num = 16 ;
			//店铺
			if (I('get.store_name')) {				
				$where =array('store_name'=>I('get.store_name'));
				$store = _getOne($this->tb_store,$where);
				$st_id = $store ['store_id'] ;
			}
			$str = "goods_verify=%d and goods_state=%d" ;
			if ($st_id) {
				$str .= " and st_id=%d" ;
				$data = array(1,1,$st_id) ;
			}
			if (I('get.goods_name')) {				
				$str ? $str .=  "and productname='%s'" : $str = "productname LIKE '%s'" ;
				$data ? $data = array(1,1,$st_id,'%'.I('get.goods_name').'%')  : $data = array(1,1,'%'.I('get.goods_name').'%');
			}
			//dump($str);
			//dump($data);die;
			//产品搜索
			if ($data) {				
				$g_list = _getList($this->tb_goods,$str,$data,'','id desc',$_GET['p'],$num) ;
				
			}
			$this->assign('g_list',$g_list) ;
			//dump($g_list);die;
			
		}
		
		$this->display('h_sales_add');
		
	}
	
	/**
	*促销区 添加/修改 操作
	*/		
	public function sales_add() {
		
		//搜索商品
		if (IS_POST && I('post.select')) {
			
			$type = I('post.type');
			$where_1 ['se_name']  = I('post.se_name') ;
			switch ($type) {
				case 'prom_add':
					$url = 'home_sales';
					I('post.se_name') ? $data['se_name'] = I('post.se_name'):$this->error('促销区名称不能为空',$url);		
					I('post.g_id') ? $data['g_id'] = I('post.g_id'):$this->error('添加商品不能为空',$url);
					I('post.se_goin') ? $data['se_goin'] = I('post.se_goin'):$data['se_goin'] = 0;					
				    $data['g_id'] = strColation($data['g_id']) ;
					$data['se_type'] = 1 ;						
					$data['se_states'] = 1;						
					$res = 1 ;
					$massege = '添加成功!' ;
					$error = '添加失败！' ;
					$table = $this->tb_prom ;
					
				break;
				case 'prom_edit':
					$url = 'home_sales';
					$where ['se_id'] = I('post.se_id');
					$url = 'home_sales';
					I('post.se_name') ? $data['se_name'] = I('post.se_name'):$this->error('促销区名称不能为空',$url);
					
					I('post.g_id') ? $data['g_id'] = I('post.g_id'):$this->error('添加商品不能为空',$url);
					I('post.se_goin') ? $data['se_goin'] = I('post.se_goin'):$data['se_goin'] = 0;					
					$data['g_id'] = strColation($data['g_id']) ;
					$data['se_type'] = 1 ;						
					$data['se_states'] = 1;						
					$res = 2 ;
					$massege = '修改成功!' ;
					$error = '修改失败！' ;
					$table = $this->tb_prom ;

					$where_1 ['se_id']  = array('neq',I('post.se_id')) ;
				break;				
			}
			
			$prom_ones = _getOne($this->tb_prom,$where_1);
			if ($prom_ones) {
				$this->error('促销区名称已存在',$url);
			}
			//dump($data);die;
			if ($res==1) {
				$change = _getAdd($table,$data);
			} else if ($res==2) {				
				$change =  _getEdit($table,$where,'',$data);

			}			
				
			unset($data);
			unset($res);
			unset($type);	
			unset($where);
			unset($table);
			
			if ($change) {
				//清楚缓存
				SClear() ;
				unset($change);
				$this->success($massege,$url);				
			} else {
				$this->error($error,$url);
			}			
		}			
		
	}	
	
}